package mytest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @program: thinkinginjava
 * @description:
 * @packagename: mytest
 * @author: xzs
 * @date: 2021-03-24 22:50
 **/
public class SemaphoreTest {
    static Semaphore test = new Semaphore(3);
    static ExecutorService es = Executors.newFixedThreadPool(4);
    static ExecutorService es1 = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        for (int i = 0; i < 4; i++) {
            es.execute(() -> {
                try {
                    test.acquire();
                    System.out.println("end");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        es1.execute(()->{
            test.release();
        });

    }

}
